import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Deom2 {//sql查询
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test1?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("88800662");

        Connection connection = dataSource.getConnection();

        String sql = ("select * from score");
        PreparedStatement statement = connection.prepareStatement(sql);

        //executeQuery();的返回值是resultSet类型
        //ResultSet是查询的结果集合(临时表)，此处就需要对结果集合进行遍历
        ResultSet resultSet = statement.executeQuery();
        //通过next就可以获得临时表中的每一行数据，如果到了最后一行在执行next就会返回fasle
        while(resultSet.next()){
            int name = resultSet.getInt("name");
            int Chinese = resultSet.getInt("Chinese");
            int English = resultSet.getInt("English");
            int math = resultSet.getInt("math");
            System.out.println("name:" + name + " Chinese:" + Chinese + " English" + English + " math:" + math);
        }

        resultSet.close();
        statement.close();
        connection.close();
    }
}
